package com.jonniespratley.flexdatabasemanager
{
	import com.jonniespratley.flexdatabasemanager.business.Services;
	import com.jonniespratley.flexdatabasemanager.control.FlexDatabaseManagerController;
	import com.jonniespratley.flexdatabasemanager.model.ModelLocator;
	import com.jonniespratley.flexdatabasemanager.services.DatabaseManagerServicer_Tester;
	
	import flash.events.ContextMenuEvent;
	import flash.events.Event;
	import flash.ui.ContextMenuItem;
	
	import fr.kapit.CairngormConsole;
	import fr.kapit.KapInspect;
	
	import mx.controls.Alert;
	import mx.core.Application;
	import mx.managers.PopUpManager;

	public class FlexDatabaseManagerCore extends Application
	{
		public function FlexDatabaseManagerCore()
		{
			super();
			
			//Initialize the Cairngorm Console before the controller
			this.registerCairngormConsole();
			
			//Initialize the Cairngorm Services
			this.registerServices();
			
			//Initialize the Cairngorm Controller
			this.registerController();
			
			//Build the layout
			this.buildLayout();
			
			this.registerKapitInspect();
		}
		
		private function registerCairngormConsole():void
		{
			new CairngormConsole( ModelLocator.getInstance(), this );
		}
		
		private function registerKapitInspect():void
		{
			new KapInspect();
		}
	
		private function registerController():void
		{
			new FlexDatabaseManagerController();
		}
		
		private function registerServices():void
		{
			new Services();	
		}
		
		private function buildLayout():void
		{
			this.layout = "vertical";
		}
		
		public function buildContextMenu():void
		{
			
		/* 
			var customMenuItem1:ContextMenuItem = new ContextMenuItem("Flex SDK " + mx_internal::VERSION, false, false);
			var customMenuItem2:ContextMenuItem = new ContextMenuItem("Player " + Capabilities.version, false, false);
			var contextMenuCustomItems:Array = application.contextMenu.customItems;
			contextMenuCustomItems.push(customMenuItem1);
			contextMenuCustomItems.push(customMenuItem2); 
		*/
        	var cairngormConsoleItem1:ContextMenuItem = new ContextMenuItem( 'Cairngorm Debug-Console', false, true, true );
         		cairngormConsoleItem1.addEventListener( ContextMenuEvent.MENU_ITEM_SELECT, openConsole );
         	var svcConsoleItem2:ContextMenuItem = new ContextMenuItem( 'FDM Service-Console', false, true, true );
         		svcConsoleItem2.addEventListener( ContextMenuEvent.MENU_ITEM_SELECT, openServiceTester );
         		
        	var contentMenuItems:Array = this.contextMenu.customItems;
        		contentMenuItems.push( cairngormConsoleItem1 );
        		contentMenuItems.push( svcConsoleItem2 );

		}
		
		
		public function openConsole( event:Event ):void
		{
			if ( event is ContextMenuEvent )
			{
				CairngormConsole.getInstance().showPopup();
			}
		}
		
		public function openInspectConsole( event:Event ):void
		{
			if ( event is ContextMenuEvent )
			{
				
			}
		}
		
		public function openServiceTester( event:Event ):void
		{
			if ( event is ContextMenuEvent )
			{
				PopUpManager.createPopUp( this, DatabaseManagerServicer_Tester );
			}
		}
		
		
		private function applicationMaintance():void
		{
			Alert.show( "We're sorry for the inconvenience, but this service is currently unavailable.\n" + 
					"We will be implementing changes to offer you an improved experience with Flex Database Manager", "Unavailable" );
		}
		
	}
}